<script type="text/javascript">
    /*<![CDATA[*/
    jQuery(function($) {
        //alert("hello");
        jQuery('a[rel="tooltip"]').tooltip();
        jQuery('a[rel="popover"]').popover();
    });
    function clickedAcceptBtn(obj)
    {
        //alert(obj.id);
        $("#" + obj.id).addClass('disabled btn-warning').text('Accepted already');
    }
    function clickedRejectBtn(obj)
    {
        //alert(obj.id);
        $("#" + obj.id).addClass('disabled btn-warning').text('Rejected already');
    }

    function clickedRemoveBtn(obj)
    {
        //alert(obj.id);
        $("#" + obj.id).addClass('disabled btn-warning').text('Removed already');
    }
</script>
<link rel="stylesheet" type="text/css"
      href="<?php echo Yii::app()->request->baseUrl; ?>/css/bootstrap-datetimepicker.min.css" />
<script  type="text/javascript"
         src="<?php echo Yii::app()->request->baseUrl; ?>/js/bootstrap-datetimepicker.min.js"/>

<script type="text/javascript">
    $(function() {
        $("#checkAll_2").click(function() {
            $('input[name="subBox"]').attr("checked", this.checked);
        });
        var $subBox = $("input[name='subBox']");
        $subBox.click(function() {
            $("#checkAll").attr("checked", $subBox.length == $("input[name='subBox']:checked").length ? true : false);
        });
    });</script>
<script type="text/javascript">
    $(function() {
        $('.tree li:has(ul)').addClass('parent_li').find(' > span').attr('title', 'Collapse this branch');
        $('.tree li.parent_li > span').on('click', function(e) {
            var children = $(this).parent('li.parent_li').find(' > ul > li');
            if (children.is(":visible")) {
                children.hide('fast');
                $(this).attr('title', 'Expand this branch').find(' > i').addClass('icon-plus-sign').removeClass('icon-minus-sign');
            } else {
                children.show('fast');
                $(this).attr('title', 'Collapse this branch').find(' > i').addClass('icon-minus-sign').removeClass('icon-plus-sign');
            }
            e.stopPropagation();
        });
    });</script>
<script type="text/javascript">
    $(function() {
        $("#Event_Shop_CX").click(function() {
            document.getElementById("Event_Shop_CX_View").style.display = '';
            document.getElementById("Event_Clothes_CX_View").style.display = 'none';
        });
        $("#Event_Clothes_CX").click(function() {
            document.getElementById("Event_Shop_CX_View").style.display = 'none';
            document.getElementById("Event_Clothes_CX_View").style.display = '';
        });
    });</script>
<?php
//$app = Blyn::app()->getActiveApp();
//$eventList = $app->getEventList();
$page = $control;
$screen = $control;
//$screen = Blyn::app()->getUIManager()->getCurrentScreen();
//$screen = Blyn::app()->getCurrentNode()->getUIManager()->getCurrentScreen();

$joinedUsers = Blyn::app()->getActiveSpace()->getSpaceEngine()->getJoinedSpaceRoles();
//$reqs = Blyn::app()->getActiveSpace()->getSpaceEngine()->getJoinSpaceRequests();
$reqs = Blyn::app()->getActiveSpace()->getSpaceEngine()->getWXJoinSpaceRequests();
//$reqs = array('1'=>array('userId'=>1, 'spaceId'=>2, 'appId'=>4,'applyingRole'=>'普通成员'),
//              '2'=>array('userId'=>2, 'spaceId'=>2, 'appId'=>5,'applyingRole'=>'管理员'));
?>
<div class=" pull-right">
    机构成员列表|
    <?php echo $control->getAjaxLink(array('name' => '管理机构角色', 'id' => BUIRole::Show_Admin_Role)); ?>|
    <?php echo $control->getAjaxLink(array('name' => '管理申请规则', 'id' => BUIMembership::Show_Admin_Membership)); ?>
</div>
<br>
<legend><H4><?php echo Yii::t('site', 'Join Space Requests'); ?>:</h4></legend>
<fieldset>
    <table class="table table-hover">
        <?php if ($reqs != NULL) { ?>
            <thead>
                <tr>
                    <th><?php echo Yii::t('site', 'Applicant'); ?></th>
                    <th><?php echo Yii::t('site', 'Space'); ?></th>
                    <th><?php echo Yii::t('site', 'Application'); ?></th>
                    <th><?php echo Yii::t('site', 'Role'); ?></th>
                    <th><?php echo Yii::t('site', 'Operation'); ?></th>
                </tr> 
            </thead>
        <?php } ?>
        <tbody>
            <?php
            if ($reqs != NULL) {
                foreach ($reqs as $reqId => $request) {
                    $sender_user = new BUser($request['userId']);
                    $sender_Space = new BSpace($request['spaceId']);
                    $sender_sApp = Yii::t('site', BApp::getAppById($request['appId'])->name);
                    $requestItem = array('messageId' => $reqId, 'roleId' => $request['roleId'], 'userId' => $request['userId']);

                    $ajaxAcceptBtn = $screen->getAjaxLink(array('name' => Yii::t('site', 'Accept'), 'id' => 'Accept' . $reqId), array('reqId' => $requestItem,
                        'htmlOptions' => array('class' => 'btn btn-small btn-primary', 'enctype' => 'multipart/form-data')));
                    $ajaxRejectBtn = $screen->getAjaxLink(array('name' => Yii::t('site', 'Reject'), 'id' => 'Reject' . $reqId), array('reqId' => $requestItem,
                        'htmlOptions' => array('class' => 'btn btn-small btn-primary', 'enctype' => 'multipart/form-data')));
                    ?>
                    <tr>
                        <td> <?php echo $sender_user->getName(); ?></td>
                        <td> <?php echo $sender_Space->getName(); ?></td>
                        <td> <?php echo $sender_sApp; ?></p>                    
                        </td>
                        <td> <?php echo Yii::t('site', $request['applyingRole']); ?> </td>
                        <td>
                            <?php echo $ajaxAcceptBtn; ?>
                            <?php echo $ajaxRejectBtn; ?>
                        </td>
                    </tr>
                    <?php
                }
            } else {
                echo Yii::t('site', 'No join space request.');
            }
            ?>
        </tbody>
    </table>

    <H4><?php echo Yii::t('site', 'Joined Space Users'); ?>:</h4>
    <table class="table table-hover">
        <?php if ($joinedUsers != NULL) { ?>
            <thead>
                <tr>
                    <th><?php echo Yii::t('site', 'Joined User'); ?></th>
                    <th><?php echo Yii::t('site', 'Space'); ?></th>
                    <th><?php echo Yii::t('site', 'Application'); ?></th>
                    <th><?php echo Yii::t('site', 'Role'); ?></th>
                    <th><?php echo Yii::t('site', 'Operation'); ?></th>
                </tr> 
            </thead>
        <?php } ?>
        <tbody>
            <?php
            if ($joinedUsers != NULL) {
                foreach ($joinedUsers as $joinedUserId => $joinedUser) {
                    $sender_user = new BUser($joinedUser['userId']);
                    $sender_Space = new BSpace($joinedUser['spaceId']);
                    $sender_sApp = Yii::t('site', BApp::getAppById($joinedUser['appId'])->name);
                    $requestItem = array('messageId' => $joinedUserId, 'roleId' => $joinedUser['roleId'], 'userId' => $joinedUser['userId']);
                    $ajaxRemoveBtn = $screen->getAjaxLink(array('name' => Yii::t('site', 'Remove'), 'id' => 'Remove' . $joinedUserId), array('reqId' => $requestItem,
                        'htmlOptions' => array('class' => 'btn btn-small btn-primary', 'enctype' => 'multipart/form-data')));
                    ?>
                    <tr>
                        <td> <?php echo $sender_user->getName(); ?></td>
                        <td> <?php echo $sender_Space->getName(); ?></td>
                        <td> <?php echo $sender_sApp; ?></p>                    
                        </td>
                        <td> <?php echo Yii::t('site', $joinedUser['applyingRole']); ?> </td>
                        <td>
                            <?php echo $ajaxRemoveBtn; ?>
                        </td>
                    </tr>

                    <?php
                }
            } else {
                echo Yii::t('site', 'No joined space User.');
            }
            ?>
        </tbody>
    </table>

